<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:gh:employee:add']"
          >动账</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="账户变动类型" align="center" prop="accountType">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.accountType == 1">充值</el-tag>
          <el-tag type="danger" v-if="scope.row.accountType == 2">消耗</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="客户账户余额-动账前"
        align="center"
        prop="customerAccountBefore"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.customerAccountBefore }}元</div>
        </template>
      </el-table-column>
      <el-table-column label="动账金额" align="center" prop="dynamicPrice">
        <template slot-scope="scope">
          <div>{{ scope.row.dynamicPrice }}元</div>
        </template>
      </el-table-column>
      <el-table-column
        label="客户账户余额-动账后"
        align="center"
        prop="customerAfterPrice"
      >
        <template slot-scope="scope">
          <div>{{ scope.row.customerAfterPrice }}元</div>
        </template>
      </el-table-column>
      <el-table-column label="所属商城" align="center" prop="childrenMallName">
      </el-table-column>

      <el-table-column label="动账方式" align="center" prop="dynamicMethod">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dynamicMethod == 1">账户转账</el-tag>
          <el-tag v-if="scope.row.dynamicMethod == 2">现金</el-tag>
          <el-tag v-if="scope.row.dynamicMethod == 3">微信</el-tag>
          <el-tag v-if="scope.row.dynamicMethod == 4">支付宝</el-tag>
          <el-tag v-if="scope.row.dynamicMethod == 5">制券</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="账户变动类型" prop="accountType">
          <el-radio-group v-model="form.accountType">
            <el-radio :label="1">充值</el-radio>
            <el-radio :label="2">消耗</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="动账金额" prop="dynamicPrice">
          <el-input-number
            v-model="form.dynamicPrice"
            :min="1"
            :precision="2"
          ></el-input-number>
        </el-form-item>

        <el-form-item label="动账方式" prop="dynamicMethod">
          <el-select v-model="form.dynamicMethod" placeholder="请选择">
            <el-option label="账户转账" :value="1"> </el-option>
            <el-option label="现金" :value="2"> </el-option>
            <el-option label="微信" :value="3"> </el-option>
            <el-option label="支付宝" :value="4"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, addAccount } from "@/api/customer/account";
export default {
  name: "Account",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      customerList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountType: [
          { required: true, message: "账户变动类型不能为空", trigger: "blur" },
        ],
        dynamicPrice: [
          { required: true, message: "动账金额不能为空", trigger: "blur" },
        ],
        dynamicMethod: [
          { required: true, message: "动账方式不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.queryParams.customerId = this.$route.query.customerId;
      listAccount(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      const customerId = this.$route.query.customerId;
      this.form = {
        accountType: 1,
        customerId,
        dynamicMethod: undefined,
        dynamicPrice: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "动账";
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addAccount(this.form).then((response) => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
